package test;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.print.Printable;

import javax.swing.JFrame;
import javax.swing.*;
public class jisuanqi extends JFrame{
	private JLabel headerLabel;
	private JLabel resultLabel;
	private JPanel controlPanel;
	private JFrame jf;
	public jisuanqi(){
		pre_gui();
	}
	public void pre_gui(){
		jf = new JFrame();
		jf.setLayout(new GridLayout(3, 1));
		jf.setTitle("计算器");
		jf.setSize(400,400);
		jf.setLocation(300, 300);
		jf.setVisible(true);
	    jf.setDefaultCloseOperation(1);
	}
	public void run() {
		
	    headerLabel = new JLabel("", JLabel.CENTER);
	    resultLabel = new JLabel("", JLabel.CENTER);   
	    controlPanel = new JPanel();
	    controlPanel.setLayout(new FlowLayout());
	    jf.add(headerLabel);
	    jf.add(controlPanel);
	    jf.add(resultLabel);
	    showLabelDemo();
	}
	private void showLabelDemo(){
	      headerLabel.setText("这是一个计算器的程序");
	      resultLabel.setText("这里显示结果"); 
	      final JTextField numOneText = new JTextField(6);
	      final JTextField numTwoText = new JTextField(6);
	      final JTextField numThreeText = new JTextField(6);
	      final DefaultComboBoxModel firstName = new DefaultComboBoxModel();
	      firstName.addElement("+");
	      firstName.addElement("-");
	      firstName.addElement("*");
	      firstName.addElement("/");
	      final JComboBox fruitCombo = new JComboBox(firstName);    
	      fruitCombo.setSelectedIndex(0);
	      final JLabel eqLable = new JLabel("=",JLabel.CENTER);
	      final  JButton actionButton = new JButton("计算"); 
	      actionButton.addActionListener(new ActionListener() {
	    	  public void actionPerformed(ActionEvent e) {
		             String data1 = numOneText.getText();
		             String data2 = numTwoText.getText();
		             String data3 = ""+fruitCombo.getItemAt(fruitCombo.getSelectedIndex());
		             Float data4 = null;
		             try {
		            	 Float.valueOf(data1);
		            	 Float.valueOf(data2);
					} catch (Exception e2) {
						// TODO: handle exception
						resultLabel.setText("请检查输入的数据格式");
						resultLabel.setForeground(Color.RED);
					}
		             switch(data3){
		                case"+":
		                	data4= Float.valueOf(data1) + Float.valueOf(data2);
		                	break;
		                case"-":
		                	data4 = Float.valueOf(data1) - Float.valueOf(data2);
		                	break;
		                case"*":
		                	data4 = Float.valueOf(data1) * Float.valueOf(data2);
		                	break;
		                case"/":
		                	data4 = Float.valueOf(data1) / Float.valueOf(data2);
		                	break;
		             }
		             numThreeText.setText(String.valueOf(data4));
		             resultLabel.setText(data1+data3 +data2 +"="+ data4);
		             resultLabel.setForeground(Color.blue);
		      } 
		});
	      controlPanel.add(numOneText);
	      controlPanel.add(fruitCombo);
	      controlPanel.add(numTwoText);
	      controlPanel.add(eqLable);
	      controlPanel.add(numThreeText);
	      controlPanel.add(actionButton);
	      jf.setVisible(true);  
	}
}